package com.paipaideli.ui.home.adapter;

import android.graphics.Rect;
import android.support.annotation.Px;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by wangJia on 2017/5/15.
 * 首页recyclrview线条
 */

public class HomePageItemDecoration extends RecyclerView.ItemDecoration {
    private int pxBetweenItems;
    private boolean verticallyScrolling;
    private boolean horizontallyScrolling;
    private boolean firstItem;
    private boolean lastItem;
    private boolean grid;

    private boolean isFirstItemInRow;
    private boolean fillsLastSpan;
    private boolean isInFirstRow;
    private boolean isInLastRow;

    public HomePageItemDecoration() {
        this(0);
    }

    public HomePageItemDecoration(@Px int pxBetweenItems) {
        setPxBetweenItems(pxBetweenItems);
    }

    public void setPxBetweenItems(@Px int pxBetweenItems) {
        this.pxBetweenItems = pxBetweenItems;
    }

    @Px
    public int getPxBetweenItems() {
        return pxBetweenItems;
    }

    public boolean istrue=true;

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        // Zero everything out for the common case
        outRect.setEmpty();

        int position = parent.getChildAdapterPosition(view);
        if (position == RecyclerView.NO_POSITION) {
            // View is not shown
            return;
        }

        RecyclerView.LayoutManager layout = parent.getLayoutManager();
        calculatePositionDetails(parent, position, layout);

        boolean left = useLeftPadding();
        boolean right = useRightPadding();
        boolean top = useTopPadding();
        boolean bottom = useBottomPadding();

        if (shouldReverseLayout(layout, horizontallyScrolling)) {
            if (horizontallyScrolling) {
                boolean temp = left;
                left = right;
                right = temp;
            } else {
                boolean temp = top;
                top = bottom;
                bottom = temp;
            }
        }


        int spanSize = 0;
        try {
            if(layout instanceof GridLayoutManager){
                GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager();
                int currentPosition = gridLayoutManager.getPosition(view);
                spanSize = gridLayoutManager.getSpanSizeLookup().getSpanSize(currentPosition);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(spanSize==1){
            outRect.top = top ? pxBetweenItems : 0;
            if(right){
                outRect.left = left ? 0 : pxBetweenItems*2;
            }else{
                outRect.left = left ? pxBetweenItems : 0;
            }
            if(left){
                outRect.right = right ? 0: pxBetweenItems*2;
            }else{
                outRect.right = right ? 0: 0;
            }
        }else {
            outRect.bottom =  0;
            outRect.top = 0;
            outRect.right = 0;
            outRect.left = 0;
        }

    }

    private void calculatePositionDetails(RecyclerView parent, int position, RecyclerView.LayoutManager layout) {
        int itemCount = parent.getAdapter().getItemCount();
        firstItem = position == 0;
        lastItem = position == itemCount - 1;
        horizontallyScrolling = layout.canScrollHorizontally();
        verticallyScrolling = layout.canScrollVertically();
        grid = layout instanceof GridLayoutManager;

        if (grid) {
            GridLayoutManager grid = (GridLayoutManager) layout;
            final GridLayoutManager.SpanSizeLookup spanSizeLookup = grid.getSpanSizeLookup();
            int spanSize = spanSizeLookup.getSpanSize(position);
            int spanCount = grid.getSpanCount();
            int spanIndex = spanSizeLookup.getSpanIndex(position, spanCount);
            isFirstItemInRow = spanIndex == 0;
            fillsLastSpan = spanIndex + spanSize == spanCount;
            isInFirstRow = isInFirstRow(position, spanSizeLookup, spanCount);
            isInLastRow =
                    !isInFirstRow && isInLastRow(position, itemCount, spanSizeLookup, spanCount);
        }
    }

    private static boolean shouldReverseLayout(RecyclerView.LayoutManager layout, boolean horizontallyScrolling) {
        boolean reverseLayout =
                layout instanceof LinearLayoutManager && ((LinearLayoutManager) layout).getReverseLayout();
        boolean rtl = layout.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL;
        if (horizontallyScrolling && rtl) {
            // This is how linearlayout checks if it should reverse layout in #resolveShouldLayoutReverse
            reverseLayout = !reverseLayout;
        }

        return reverseLayout;
    }

    private boolean useBottomPadding() {
        if (grid) {
            return (horizontallyScrolling && !fillsLastSpan)
                    || (verticallyScrolling && !isInLastRow);
        }

        return verticallyScrolling && !lastItem;
    }

    private boolean useTopPadding() {
        if (grid) {
            return (horizontallyScrolling && !isFirstItemInRow)
                    || (verticallyScrolling && !isInFirstRow);
        }

        return verticallyScrolling && !firstItem;
    }

    private boolean useRightPadding() {
        if (grid) {
            return (horizontallyScrolling && !isInLastRow)
                    || (verticallyScrolling && !fillsLastSpan);
        }

        return horizontallyScrolling && !lastItem;
    }

    private boolean useLeftPadding() {
        if (grid) {
            return (horizontallyScrolling && !isInFirstRow)
                    || (verticallyScrolling && !isFirstItemInRow);
        }

        return horizontallyScrolling && !firstItem;
    }

    private static boolean isInFirstRow(int position, GridLayoutManager.SpanSizeLookup spanSizeLookup, int spanCount) {
        int totalSpan = 0;
        for (int i = 0; i <= position; i++) {
            totalSpan += spanSizeLookup.getSpanSize(i);
            if (totalSpan > spanCount) {
                return false;
            }
        }

        return true;
    }

    private static boolean isInLastRow(int position, int itemCount, GridLayoutManager.SpanSizeLookup spanSizeLookup,
                                       int spanCount) {
        int totalSpan = 0;
        for (int i = itemCount - 1; i >= position; i--) {
            totalSpan += spanSizeLookup.getSpanSize(i);
            if (totalSpan > spanCount) {
                return false;
            }
        }

        return true;
    }
}